<?php
	
	// Session
	session_start();
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/register.html');
	define('CAT',"members");
	
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	// OPTIONS
	$tmp->setOptions($_POST);
	
	// LABELS
	$tmp->setLabels();
	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members")); 
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	
	
	// Permbajtja e faqes %content%
	$tmp->setContent("HELP",$tmp->xmlContent("register"));

	
	// Zevendeso simbolet per Hyperlink mbrenda emertimeve p.sh. [AGREEMENT]
	$tmp->vorlage = $tmp->xmlLink($tmp->vorlage);
	
	
	// Aplikimi i formes
	if(isset($_POST['email'])) {
		// POST vars
		$sex = $_POST['sex'];
		$name = trim($_POST['name']);
		$place = trim($_POST['place']);
		$state = $_POST['state'];
		$date = $_POST['date'];
		$month = $_POST['month'];
		$year = $_POST['year'];
		$profession = trim($_POST['profession']);
		$nickname = trim($_POST['nickname']);
		$email = trim($_POST['email']);
		$password = trim($_POST['password']);
		$password2 = trim($_POST['password2']);
		$agreement = $_POST['agreement'];
		$anonym = $_POST['anonym'];
		$sendnews = $_POST['sendnews'];
		
		// Lidhu me arkiven
		dbOpen('comunity');
		
		// NULL NAME 
		if(empty($name)) 
			$ERROR['name'] = "null";
		// SYNTAX NAME 
		elseif(!eregi("^[a-zA-Z]+[[:space:]]+[a-zA-Z]{2,20}$",$name)) 
			$ERROR['name'] = "syntax";
		// DOUBLE NAME
		else {
			
			// Kontrollo a ekziston emri i njejte ne ariven e vizitoreve
			$query = mysql_query("SELECT id FROM users WHERE name = '$name'");
			
			if (mysql_num_rows($query) > 0) 
				$ERROR['name'] = "double";
		}
		
		// NULL CITY
		if(empty($place)) 
			$ERROR['place'] = "null";
		elseif(is_numeric($place))
			$ERROR['place'] = "value";
		
		// NULL STATE
		if(empty($state))
			$ERROR['state'] = "null";
		
		// NULL DAY
		if(empty($date) || empty($month) ||empty($year) )
			$ERROR['birthdate'] = "null";

		// VALUE YEAR
		if((date(Y) - $year) < 18) 
			$ERROR['year'] = "value";
		
		// NULL SEX
		if(empty($sex))
			$ERROR['sex'] = "null";
		
		// NULL PROFESSION
		if(empty($profession))
			$ERROR['profession'] = "null";
		
		// NULL NICKNAME
		if(empty($nickname))
			$ERROR['nickname'] = "null";
		// DOUBLE NICKNAME
		else {
			// Kontrollo valle a ekziston pseudonimi i kerkuar
			$query = mysql_query("SELECT id FROM users WHERE nickname = '$nickname'");
			
			if (mysql_num_rows($query) > 0)
				$ERROR['nickname'] = "double";
		}
		
		// NULL EMAIL
		if(empty($email)) 
			$ERROR['email'] = "null";
		// SYNTAX EMAIL
		elseif(!ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $email))
			$ERROR['email'] = "syntax";
		else {
			// Kontrollo valle a ekziston emaili ne arkiven e vizitoreve
			$query = mysql_query("SELECT id FROM users WHERE email = '$email'");
			
			if (mysql_num_rows($query) > 0)
				$ERROR['email'] = "double";
		}
		
		// PASSWORD
		if(empty($password)) 
			$ERROR['password'] = "null";
		elseif(strlen($password) > 16 || strlen($password) < 6)
			$ERROR['password'] = "syntax";
			
		// NULL PASSWORD2 
        if(empty($password2)) 
			$ERROR['password2'] = "null";
		// VALUE PASSWORD2
		elseif($password !== $password2)
			$ERROR['password2'] = "value";
			
		// NULL AGREEMENT
		if(empty($agreement))
			$ERROR['agreement'] = "null";
		
		// Nese nuk ka asnje gabim
		if(empty($ERROR)) {
			
			// MySQL date foormat
			$birthdate = $year."-".$month."-".$date;
			$birthdate = strtotime($birthdate);
			$regdate = $_SERVER['REQUEST_TIME'];
			
			// Numri konfirmues i gjeneruar ne baze te momentit registrues,
			// Arkivohet ne fushen e "street", dhe pas konfirmimit, kjo fushe lirohet nga kjo vlere
			$cid = $_SERVER['REQUEST_TIME'];
			
			// Arkivimi i vlerave
			$query = "INSERT INTO users SET 
								sex = '$sex',
								name = '$name',
								street = '$cid',
								place = '$place',
								profession = '$profession',
								state = '$state',												
								birthdate = '$birthdate',
								nickname = '$nickname',
								email = '$email',
								password = '$password',
								sendnews = '$sendnews',
								anonym = '$anonym',
								regdate = '$regdate',
								status = '1',
								style = 'default' ";
			
			
			// Nese insertimi ka sukses dergo emailin per konfirmim te llogaris
			if(mysql_query($query)) {
				
				// Inserto ne ne author vizitorin si autor
				mysql_query("INSERT INTO authors SET name = '$name'");
				
				// MAIL TEMPLATE
				$tmpMail = new Template("../include/styles/default/sendmail/confirm.html",$PROJECT['lang']);
				
				// XML INCLUDING
				$path = "../include/lang/".$PROJECT['lang']."/mail.xml";
				$xmlMail = simplexml_load_file($path);
				
				// Ndyshoret per zevendesim ne HTML shabllon
				$USER['subject'] = html_encrypt($xmlMail->confirm->subject);
				$USER['noreplay'] = nl2br($xmlMail->noreplay->body);
				$USER['content'] = nl2br($tmpMail->xmlLink($xmlMail->confirm->body));
				$USER['senddate'] = setDate();
				
				// Konverto ndryshoret ne HTML shabllon
				$tmpMail->setContent($USER);
				
				// Ndyshoret per zevendesim ne XML shabllon
				$USER['name'] = $name;
				$USER['cid'] = $cid;
				$USER['sex'] = ($sex == 1) ? "|mister|" : "|misses|";
				
				// Konverto ndryshoret ne shabllonin e Acountit
				$tmpMail->setXMLVars($USER);
				
				
				// Konverto emrtimet ne shabllon
				$tmpMail->setLabels();
				
				// HEADERS				
				$header = 'MIME-Version: 1.0' . "\r\n";
				$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";					
				$header .= "From: ".$PROJECT['name']." <".$PROJECT['noreplay'].">\r\n";
				
				// SEND MAIL
				mail($email, $USER['subject'], $tmpMail->vorlage, $header);
				
				// Rilexo faqen me porosin per sukses
				header('Location: ../message.php?sid=register&id=success'); 
				exit;
			}
			else {
				
				// Rilexo faqen me porosin per sukses
				header('Location: ../message.php?sid=register&id=error'); 
				exit;
			}
		}
	}
	
	// Convert POST and ERROR vars in Form
	$tmp->setForm($_POST,$ERROR);

	
	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;
	
?>